var express = require('express');
var router = express.Router();
const connected = require("../../mongo/mongoose_connect");
const Models = require("../../mongo/mongoose_schemas");

router.get("/preview", async (req, res) => {
  await connected;
  let q_EventStats = Models.Event.collection.stats();
  let q_PlayerStats = Models.Player.collection.stats();
  let q_MatchStats = Models.Match.collection.stats();
  let o_statses = await Promise.all([q_EventStats, q_MatchStats, q_PlayerStats]);
  res.send({
    event_stats: o_statses[0],
    match_stats: o_statses[1],
    palyer_stats: o_statses[2]
  })
})

router.get("/players/p/:page", async (req, res) => {

});

router.get("/characters/:match_id", async (req, res) => {
  await connected;
  let match_id = req.params['match_id'];
  // console.log("match_id",match_id);
  let o_query = await Models.PlayerCharacters.find({
    match_id: match_id
  });
  // console.log("data length",o_query.length);
  for (let model of o_query) {
    let charas = model._doc.arrayOfCharacters;
    charas = charas.filter(e => {
      let loc = e.character.location;
      if (loc.x == 0 && loc.y == 0 && loc.z == 0) {
        return false
      }
      return true;
    });
    model._doc.arrayOfCharacters = charas;
  }
  // debugger
  return res.send(o_query);
});

router.get("/match/:match_id", async (req, res) => {
  await connected;
  let match_id = req.params['match_id'];
  let o_match = await Models.Match.findOne({
    match_id: match_id
  });
  res.send(o_match._doc)

});

router.get("/tiaosan/Miramar",async (req,res)=>{
  
})



module.exports = router;